import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login/index.vue"),
  },
  {
    path: "/",
    name: "home",
    component: () => import("../views/home/index.vue"),
    children: [
      //   TL authority模块路径
      {
        path: "/authority/admin",
        name: "Administrator",

        component: () => import("../views/authority/Administrator"),
      },
      {
        path: "/dashboard",
        name: "shouye",

        component: () => import("../views/shouye/index.vue"),
      },
      {
        path: "/authority/button",
        name: "Button-management",

        component: () => import("../views/authority/Button-management"),
      },
      {
        path: "/authority/role",
        name: "management-user",

        component: () => import("../views/authority/management-user"),
      },
      {
        path: "/authority/categoryMenu",
        name: "/authority/categoryMenu",

        component: () => import("../views/authority/management-menu"),
      },
      //  YJB  system  模块路径
      {
        path: "/system/webConfig",
        name: "webConfig",
        component: () => import("@/views/system/webConfig"),
      },
      {
        path: "/system/aboutMe",
        name: "aboutMe",
        component: () => import("@/views/system/aboutMe"),
      },
      // WYR   blog模块路径
      {
        path: "/blog/blogTag",
        name: "blogTag",
        component: () => import("../views/blog/blogTag"),
      },
      {
        path: "/blog/blogSort",
        name: "blogList",
        component: () => import("../views/blog/blogSort"),
      },
      {
        path: "/blog/blogList",
        name: "blogCart",
        component: () => import("../views/blog/blogList"),
      },
      //  YW user 模块路径
      {
        path: "/user/user",
        name: "users",
        component: () => import("../views/user/Users.vue"),
      },
      // QRZ  message 模块路径
      {
        path: "/message/feedback",
        name: "message-feedback",
        component: () => import("../views/message/feedback.vue"),
      },
      {
        path: "/message/comment",
        name: "message-comment",
        component: () => import("../views/message/comment.vue"),
      },
    ],
    // children: [
    //   {
    //     path: "/authority/Administrator",
    //     name: "Administrator",
    //     component: () => import("../views/authority/Administrator"),
    //   },
    //   {
    //     path: "/authority/Button-management",
    //     name: "Button-management",
    //     component: () => import("../views/authority/Button-management"),
    //   },
    //   {
    //     path: "/authority/management-user",
    //     name: "management-user",
    //     component: () => import("../views/authority/management-user"),
    //   },
    //   {
    //     path: "/authority/management-menu.vue",
    //     name: "management-menu",
    //     component: () => import("../views/authority/management-menu.vue"),
    //   },
    //   {
    //     path: "/blog/blogTag",
    //     name: "blogTag",
    //     component: () => import("../views/blog/blogTag"),
    //   },
    //   {
    //     path: "/blog/blogSort",
    //     name: "blogList",
    //     component: () => import("../views/blog/blogSort"),
    //   },
    //   {
    //     path: "/blog/blogList",
    //     name: "blogCart",
    //     component: () => import("../views/blog/blogList"),
    //   },
    //   {
    //     path: "/authority/admin",
    //     name: "Administrator",

    //     component: () => import("../views/authority/Administrator"),
    //   },
    //   {
    //     path: "/dashboard",
    //     name: "shouye",

    //     component: () => import("../views/shouye/index.vue"),
    //   },
    //   {
    //     path: "/authority/button",
    //     name: "Button-management",

    //     component: () => import("../views/authority/Button-management"),
    //   },
    //   {
    //     path: "/authority/role",
    //     name: "management-user",

    //     component: () => import("../views/authority/management-user"),
    //   },
    //   {
    //     path: "/authority/categoryMenu",
    //     name: "/authority/categoryMenu",

    //     component: () => import("../views/authority/management-menu.vue"),
    //   },
    // ],
  },
];

const router = new VueRouter({
  routes,
});

// 用户在访问任何页面之前，我们一定要做个判断，再让用户跳转； 用户路由权限的控制
// vue-router官网里面提供
router.beforeEach((to, from, next) => {
  // 对于全部页面要做一个校验，只有登录过后才能进入我们的主页，否则不让进去
  // tokenStr这个单词专门用来判断用户 是否已经登录了，如果有值就登录，如果没有证明用户没有登录
  const tokenStr = window.localStorage.getItem("Token_Key");
  console.log(to, from);
  // 这个函数 要是不写没问题，如果写了，就一定要写一个 next() 相当于放行的意思

  if (to.path === "/login") return next();
  if (!tokenStr) return next("/login");
  next();
});

export default router;
